package com.mqtt.config;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MqttCallbackHandler implements MqttCallbackExtended {
    
    @Autowired
    private MqttConfig mqttConfig;

    @Override
    public void connectComplete(boolean reconnect, String serverURI) {
        log.info("MQTT连接已建立 - reconnect: {}, serverURI: {}", reconnect, serverURI);
    }

    @Override
    public void connectionLost(Throwable cause) {
        log.error("MQTT连接断开，原因: ", cause);
        // 连接丢失时记录日志，但不需要手动重连，因为设置了automaticReconnect
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        // 这个方法不会被调用，因为使用的是Spring Integration适配器
        log.info("收到消息 - topic: {}, message: {}", topic, new String(message.getPayload()));
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        // 消息发送完成
        log.debug("消息发送完成 - token: {}", token.getMessageId());
    }
}